﻿using System;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Ink;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using System.Collections.Generic;
using SilverlightApplication1.ServiceReference1;

namespace SilverlightApplication1
{
    public class ServiceGetCityData
    {

        private List<SilverlightApplication.CityData> _cities = new List<SilverlightApplication.CityData>();
        ServiceReference1.Service1SoapClient _client = new SilverlightApplication1.ServiceReference1.Service1SoapClient();

        public ServiceGetCityData (string country)
	    {
            _client.GetCityDataCompleted += new EventHandler<GetCityDataCompletedEventArgs>(client_GetCityDataCompleted);
            _client.CloseCompleted += new EventHandler<System.ComponentModel.AsyncCompletedEventArgs>(_client_CloseCompleted);

            _client.ChannelFactory.Open();
            _client.ChannelFactory.
            _client.GetCityDataAsync("Thailand");
            _client.ChannelFactory.Close();
	    }

        void _client_CloseCompleted(object sender, System.ComponentModel.AsyncCompletedEventArgs e)
        {
          
        }

        public System.ServiceModel.CommunicationState State {
            get
            {
                return _client.State;
            }
        }

        public List<SilverlightApplication.CityData> Returned
        {
            get
            {
                return _cities;
            }
        }

        void client_GetCityDataCompleted(object sender, GetCityDataCompletedEventArgs e)
        {
            foreach (var i in e.Result)
            {
                _cities.Add(
                        new SilverlightApplication.CityData(i.CityName,i.Latitude,i.Longitude) 
                    );
            }      
        }

    }
}
